﻿using System;
using Insesoft.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace InseSoft.Tests.Unit.Entities
{
    [TestClass]
    public class EntidadBaseTest
    {
        #region TestClass
        internal class TestClass : EntidadBase
        {
            private string stringProperty;
            public string StringProperty
            {
                get { return stringProperty; }
                set
                {
                    CheckLength("StringProperty", value, 10);
                    isChanged |= (stringProperty != value); stringProperty = value;
                }
            }
        }
        #endregion

        [TestMethod]
        public void IsChanged_JustCreatedEntity_DefaultsFalse()
        {
            var entity = new TestClass();

            var result = entity.IsChanged;

            Assert.AreEqual(false, result);
        }

        [TestMethod]
        public void IsChanged_OnePropertyChanged_ReturnsTrue()
        {
            var newValue = DateTime.Now.Ticks.ToString();
            var entity = new TestClass { StringProperty = newValue };

            var result = entity.IsChanged;

            Assert.AreEqual(true, result);
        }

        [TestMethod]
        public void IsChanged_OnePropertyChangedToSameValue_ReturnsFalse()
        {
            var entity = new TestClass { StringProperty = null };
            var result = entity.IsChanged;

            Assert.AreEqual(false, result);
            Assert.AreEqual(entity.StringProperty, null);
        }

        [TestMethod]
        public void CheckLength_ShouldCheckValueFalse_NoExceptionThrown()
        {
            const string longerThan10Chars = "longer than 10 characters";
            var entity = new TestClass
                             {
                                 ShouldCheckLength = false,
                                 StringProperty = longerThan10Chars
                             };

            Assert.AreEqual(longerThan10Chars, entity.StringProperty);
        }

        [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException), "Valor inválido para StringProperty")]
        public void CheckLength_TooLongInput_ExceptionThrown()
        {
            const string longerThan10Chars = "longer than 10 characters";
            new TestClass { ShouldCheckLength = true, StringProperty = longerThan10Chars };
        }

        [TestMethod]
        public void Equals_SameId_ReturnsTrue()
        {
            var entity1 = new TestClass { Id = 1, StringProperty = "Text 1" };
            var entity2 = new TestClass { Id = 1, StringProperty = "Text 2" };

            Assert.IsTrue(entity1.Equals(entity2));
        }

        [TestMethod]
        public void Equals_DifferentId_ReturnsFalse()
        {
            var entity1 = new TestClass { Id = 1, StringProperty = "Text" };
            var entity2 = new TestClass { Id = 2, StringProperty = "Text" };

            Assert.IsFalse(entity1.Equals(entity2));
        }

        [TestMethod]
        public void GetHashCode_SameId_ReturnsSameResult()
        {
            var entity1 = new TestClass { Id = 1, StringProperty = "Text 1" };
            var entity2 = new TestClass { Id = 1, StringProperty = "Text 2" };

            Assert.AreEqual(entity1.GetHashCode(), entity2.GetHashCode());
        }

        [TestMethod]
        public void GetHashCode_DifferentId_ReturnsDifferentResult()
        {
            var entity1 = new TestClass { Id = 1, StringProperty = "Text" };
            var entity2 = new TestClass { Id = 2, StringProperty = "Text" };

            Assert.AreNotEqual(entity1.GetHashCode(), entity2.GetHashCode());
        }

        [TestMethod]
        public void Prueba()
        {
            
        }

    }
}
